import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * Test client for registering nodes to system.
 * @author the_james_marq
 *
 */
public class TestClient {
    
    /**
	 * Test client creates ten nodes.
	 * 
	 * @param args Command line arguments (registry hostname, registry port, registry name, nodes to create)
	 * @throws NotBoundException 
	 */
	public static void main(String[] args) {
        Registry registry = null;
		try {
			registry = LocateRegistry.getRegistry(args[0], Integer.parseInt(args[1]));
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        RemoteNodeRegistry stub = null;
		try {
			stub = (RemoteNodeRegistry) registry.lookup(args[2]);
		} catch (AccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NotBoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
        
        for(int i = 0; i<Integer.parseInt(args[3]) ; i++) {
        	
        	System.out.println("registering new node ");
        	HostPort hp = new HostPort("localhost", 8000+i*i);

        	(new Thread(new NodeActual(stub, hp))).start();
        	try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }

		System.out.println("test client finished.");
		
		System.exit(0);
		
		
	}

}
